﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace Anuntiomatic
{
    public partial class FrmThanhToan : FrmBase
    {
        string Email;
        string TelCo;
        public string Result { get; private set; }
        public FrmThanhToan(string email)
        {
            InitializeComponent();
            this.Email = email;
            this.Load += new EventHandler(FrmThanhToan_Load);
        }

        void FrmThanhToan_Load(object sender, EventArgs e)
        {
            ddlNhaMang.Items.AddRange(
                new NameValue[]{
                    new NameValue("VIETTEL","VietTel"),
                    new NameValue("VNP","Vinaphone"),
                    new NameValue("VMS","Mobiphone"),
                    new NameValue("VCOIN","Vcoin"),
                    new NameValue("GATE","Gate"),
                }
            );
            ddlNhaMang.SelectedIndexChanged += new EventHandler(ddlNhaMang_SelectedIndexChanged);
            ddlNhaMang.SelectedIndex = 0;
        }

        void ddlNhaMang_SelectedIndexChanged(object sender, EventArgs e)
        {
            TelCo = ((NameValue)ddlNhaMang.SelectedItem).DataValue;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string pincard = txtPinCard.Text.Trim();
                string serialcard = txtSerialCard.Text.Trim();
                if (string.IsNullOrEmpty(pincard))
                    throw new Exception("Số pin không được để trống");
                if (string.IsNullOrEmpty(serialcard))
                    throw new Exception("Số serial không được để trống");
                ResponseInfo result = JsonConvert.DeserializeObject<ResponseInfo>(webservice.Charging(Email, TelCo, pincard, serialcard).AESDecryptString(encryptKey, iv));
                if (result.Errorcode.Equals("00"))
                {
                    Result = result.Message;
                    this.DialogResult = DialogResult.Yes;
                }
                else
                {
                    throw new Exception(result.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    internal class NameValue
    {

        private string dataName;
        private string dataValue;

        public NameValue(string dataValue, string dataName)
        {
            DataName = dataName;
            DataValue = dataValue;
        }

        public string DataName
        {
            get { return dataName; }
            set { dataName = value; }
        }

        public string DataValue
        {
            get { return dataValue; }
            set { dataValue = value; }
        }

        public override string ToString()
        {
            return dataName;
        }
    }
}
